/*
 website: http://www.daterangepicker.com/


 模板使用示例：
 <input ims-select-range-time="range_date">
 控制器配置：
 $scope.range_date = {
 startDate: $scope.search.startDate, //开始时间戳，精确到秒
 endDate: $scope.search.endDate,  // 结束时间戳，精确到秒
 onSelected: function (data, scope) {  //时间选择确认事件
 $scope.search.page = 1;
 $scope.search.startDate = data.startDate;
 $scope.search.endDate = data.endDate;
 $state.go('app.manage.discussion', $scope.search, {reload: true});
 }
 };
 */
angular.module('imsApp').directive('imsSelectRangeTime', [function () {
    return {
        restrict: 'AE',
        require: '?ngModel',
        replace: true,
        templateUrl: 'templates/directives/select_range_time.html',
        scope: {
            imsSelectRangeTime: '='
        },
        link: function (scope, element, $attributes, ngModel) {
            var input= $(element).find('input:first');
            input.daterangepicker({
                ranges: {
                    '今天': [moment(), moment()],
                    '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                    '过去七天': [moment().subtract(6, 'days'), moment()],
                    '过去30天': [moment().subtract(29, 'days'), moment()],
                    '这个月': [moment().startOf('month'), moment().endOf('month')],
                    '上个月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                },
                format: $attributes.format || 'YYYY/MM/DD',
                locale: {
                    applyLabel: "确定",
                    cancelLabel: "清空",
                    customRangeLabel: '时间段',
                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                    monthNames: ['一月', '二月', '三曰', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    firstDay: 1
                }
            });
            input.on('apply.daterangepicker', function (ev, picker) {
                if (scope.imsSelectRangeTime && scope.imsSelectRangeTime.onSelected) {
                    scope.imsSelectRangeTime.onSelected({
                        startDate: picker.startDate.format('X'),
                        endDate: picker.endDate.format('X')
                    }, scope);
                }
            });
            input.on('cancel.daterangepicker', function(ev, picker) {
                $(this).val('');
                scope.imsSelectRangeTime.onSelected({
                    startDate: null,
                    endDate: null
                }, scope);
            });
            scope.$watch('imsSelectRangeTime.startDate', function (n, o) {
                if (n) {
                    input.data('daterangepicker').hideCalendars();//隐藏面板
                    input.data('daterangepicker').setStartDate(moment(n * 1000));
                }
            });
            scope.$watch('imsSelectRangeTime.endDate', function (n, o) {
                if (n) {
                    input.data('daterangepicker').hideCalendars();
                    input.data('daterangepicker').setEndDate(moment(n * 1000));
                }
            });


        }
    };
}]);
